package com.example.pattern.singleton;

import com.example.pattern.singleton.LazyInnerClassSingleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//反射破坏单例
public class LazyInnerClassSingletonTest {

    public static void main(String[] args) throws NoSuchMethodException
            , InvocationTargetException, InstantiationException, IllegalAccessException {
        //很无聊的情况下，进行破坏
        Class<LazyInnerClassSingleton> clazz = LazyInnerClassSingleton.class;

        //通过反射获取私有的构造方法
        Constructor<LazyInnerClassSingleton> constructor = clazz.getDeclaredConstructor(null);
        //强制访问
        constructor.setAccessible(true);
        //暴力初始化
        LazyInnerClassSingleton s1 = constructor.newInstance();
        //调用了两次构造方法，相当于 new 了两次
        LazyInnerClassSingleton s2 = constructor.newInstance();

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s1 == s2);

    }
}
